package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.*;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Inquiry;
import com.b2b.model.entity.InquiryMaterial;
import com.b2b.util.InquiryUtil;
import com.b2b.util.QuotationGoodsUtil;
import com.b2b.util.QuotationUtil;
import com.b2b.util.ReceivingInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inquiry")
@PropertySource("classpath:config.properties")
public class InquiryController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;
    private final InquiryUtil inquiryUtil;
    private final QuotationUtil quotationUtil;
    @Autowired
    public InquiryController(InquiryUtil inquiryUtil,QuotationUtil quotationGoodsUtil,ReceivingInfoUtil receivingInfoUtil){
        this.inquiryUtil = inquiryUtil;
        this.quotationUtil = quotationGoodsUtil;
    }

    @PostMapping("/add")
    public ResponseBean insertInquiry(@RequestBody InquiryListDto inquiryListDto){
        Integer count = inquiryUtil.insertInquiryList(inquiryListDto);
        if (count <= 0){
            throw new CustomException("插入失败(Query Failure)");
        }
        Inquiry inquiry = inquiryUtil.getInquiry(inquiryListDto);
        inquiry.setInquiryId(count);
        return new ResponseBean(HttpStatus.OK.value(), "插入成功(Query was successful)", inquiry);
    }



    @GetMapping("/getDetail")
    public ResponseBean getInquiry(@RequestParam Integer inquiryId){
        Map<String,Object> result = inquiryUtil.selectByInquiry(inquiryId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


    @GetMapping("/getAll")
    public ResponseBean getAll(InquiryConditionDto baseDto){
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<InquiryRoughDto> inquiryRoughDtoList = inquiryUtil.getByCondition(baseDto);
        PageInfo<InquiryRoughDto> selectPage = new PageInfo<InquiryRoughDto>(inquiryRoughDtoList);
        if (inquiryRoughDtoList == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/getAllByC")
    public ResponseBean getRoughByC(InquiryDto inquiryDto){
        BaseDto baseDto = new BaseDto();
        baseDto.setRows(inquiryDto.getRows());
        baseDto.setPage(inquiryDto.getPage());
        baseDto.setSidx(inquiryDto.getSidx());
        baseDto.setSord(inquiryDto.getSord());
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        Integer companyId = inquiryDto.getCompanyId();
        List<InquiryRoughDto> inquiries = inquiryUtil.getRoughByC(companyId);
        PageInfo<InquiryRoughDto> selectPage = new PageInfo<InquiryRoughDto>(inquiries);
        if (inquiries == null || inquiries.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Query was successful)", result);
    }


    @DeleteMapping
    public ResponseBean deleteInquiry( InquiryMaterial inquiryMaterial){
        Integer inquiryId = inquiryMaterial.getInquiryId();
        Integer count = inquiryUtil.deleteInquiry(inquiryId);
        if (count <= 0 ){
            throw new CustomException("删除失败(Query Failure)");
        }
        Integer result = quotationUtil.deleteForInquiry(inquiryId);
        if (result <= 0 ){
            throw new CustomException("删除失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Query was successful)", null);
    }
    @PutMapping
    public ResponseBean updateInquiry(@RequestBody Inquiry inquiry){
        Integer count = inquiryUtil.updateInquiry(inquiry);
        if (count <= 0 ){
            throw new CustomException("更新失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Query was successful)", null);
    }
}
